home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / diff.zip / UTIL.C < prev    next >
Text File  |  1994-07-16  |  20KB  |  846 lines

  1. /* Support routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21. This port is also distributed under the terms of the GNU General
  22. Public License as published by the Free Software Foundation.
  23.  
  24. Please note that this file is not identical to the original GNU release,
  25. you should have received this code as patch to the official release.
  26.  
  27. $Header: e:/gnu/diff/RCS/util.c 1.15.0.2 91/03/12 17:06:46 tho Exp $  */
  28.  
  29. #define error tmp_error
  30. #define    message tmp_message
  31. #define debug_script tmp_debug_script
  32. #include "diff.h"
  33. #undef error
  34. #undef message
  35. #undef debug_script
  36. #include "regex.h"            /* was missing [tho] */
  37.  
  38. #ifdef MSDOS
  39. #include <conio.h>
  40. static char *_pipe_file (void);
  41. static void cleanup_pipe (void);
  42. #endif /* MSDOS */
  43.  
  44. /* Use when a system call returns non-zero status.
  45.    TEXT should normally be the file name.  */
  46.  
  47. void
  48. perror_with_name (text)
  49.      char *text;
  50. {
  51.   fprintf (stderr, "%s: ", program);
  52.   perror (text);
  53. }
  54.  
  55. /* Use when a system call returns non-zero status and that is fatal.  */
  56.  
  57. void
  58. pfatal_with_name (text)
  59.      char *text;
  60. {
  61.   print_message_queue ();
  62.   fprintf (stderr, "%s: ", program);
  63.   perror (text);
  64.   exit (2);
  65. }
  66.  
  67. /* Print an error message from the format-string FORMAT
  68.    with args ARG1 and ARG2.  */
  69.  
  70. void
  71. error (format, arg, arg1)
  72.      char *format;
  73.      char *arg;
  74.      char *arg1;
  75. {
  76.   fprintf (stderr, "%s: ", program);
  77.   fprintf (stderr, format, arg, arg1);
  78.   fprintf (stderr, "\n");
  79. }
  80.  
  81. /* Print an error message containing the string TEXT, then exit.  */
  82.  
  83. void
  84. fatal (message)
  85.      char *message;
  86. {
  87.   print_message_queue ();
  88.   error (message, "");
  89.   exit (2);
  90. }
  91.  
  92. #ifdef MSDOS
  93. void
  94. fatal_with_name (char *message,char *name)
  95. {
  96.   print_message_queue ();
  97.   error ("%s: %s", name, message);
  98.   exit (2);
  99. }
  100. #endif
  101.  
  102. /* Like printf, except if -l in effect then save the message and print later.
  103.    This is used for things like "binary files differ" and "Only in ...".  */
  104.  
  105. void
  106. message (format, arg1, arg2)
  107.      char *format, *arg1, *arg2;
  108. {
  109.   if (paginate_flag)
  110.     {
  111.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  112.       if (msg_chain_end == 0)
  113.     msg_chain = msg_chain_end = new;
  114.       else
  115.     {
  116.       msg_chain_end->next = new;
  117.       msg_chain_end = new;
  118.     }
  119.       new->format = format;
  120.       new->arg1 = concat (arg1, "", "");
  121.       new->arg2 = concat (arg2, "", "");
  122.       new->next = 0;
  123.     }
  124.   else
  125.     printf (format, arg1, arg2);
  126. }
  127.  
  128. /* Output all the messages that were saved up by calls to `message'.  */
  129.  
  130. void
  131. print_message_queue ()
  132. {
  133.   struct msg *m;
  134.  
  135.   for (m = msg_chain; m; m = m->next)
  136.     printf (m->format, m->arg1, m->arg2);
  137. }
  138.  
  139. /* Call before outputting the results of comparing files NAME0 and NAME1
  140.    to set up OUTFILE, the stdio stream for the output to go to.
  141.  
  142.    Usually, OUTFILE is just stdout.  But when -l was specified
  143.    we fork off a `pr' and make OUTFILE a pipe to it.
  144.    `pr' then outputs to our stdout.  */
  145.  
  146. void
  147. setup_output (name0, name1, depth)
  148.      char *name0, *name1;
  149.      int depth;
  150. {
  151.   char *name;
  152.  
  153.   /* Construct the header of this piece of diff.  */
  154.   name = (char *) xmalloc (strlen (name0) + strlen (name1)
  155.                + strlen (switch_string) + 15);
  156.  
  157.   strcpy (name, "diff");
  158.   strcat (name, switch_string);
  159.   strcat (name, " ");
  160.   strcat (name, name0);
  161.   strcat (name, " ");
  162.   strcat (name, name1);
  163.  
  164.   if (paginate_flag)
  165.     {
  166.  
  167. #ifdef MSDOS
  168.  
  169.       if ( !(outfile = fopen(_pipe_file (), "w")) )
  170.         {
  171.       error ("can't pipe to more, using stdout instead.");
  172.       outfile = stdout;
  173.         }
  174.  
  175. #else
  176.  
  177.       int pipes[2];
  178.       int desc;
  179.  
  180.       /* For a `pr' and make OUTFILE a pipe to it.  */
  181.       if (pipe (pipes) < 0)
  182.     pfatal_with_name ("pipe");
  183.  
  184.       fflush (stdout);
  185.  
  186.       desc = vfork ();
  187.       if (desc < 0)
  188.     pfatal_with_name ("vfork");
  189.  
  190.       if (desc == 0)
  191.     {
  192.       close (pipes[1]);
  193.       if (pipes[0] != fileno (stdin))
  194.         {
  195.           if (dup2 (pipes[0], fileno (stdin)) < 0)
  196.         pfatal_with_name ("dup2");
  197.           close (pipes[0]);
  198.         }
  199.  
  200.       if (execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0) < 0)
  201.         pfatal_with_name (PR_FILE_NAME);
  202.     }
  203.       else
  204.     {
  205.       close (pipes[0]);
  206.       outfile = fdopen (pipes[1], "w");
  207.     } 
  208.  
  209. #endif /* MSDOS */
  210.  
  211.     }
  212.   else
  213.     {
  214.  
  215.       /* If -l was not specified, output the diff straight to `stdout'.  */
  216.  
  217.       outfile = stdout;
  218.  
  219.       /* If handling multiple files (because scanning a directory),
  220.      print which files the following output is about.  */
  221.       if (depth > 0)
  222.     printf ("%s\n", name);
  223.     }
  224.  
  225.   free (name);
  226. }
  227.  
  228. /* Call after the end of output of diffs for one file.
  229.    Close OUTFILE and get rid of the `pr' subfork.  */
  230.  
  231. void
  232. finish_output ()
  233. {
  234.   if (outfile != stdout)
  235.     {
  236. #ifdef MSDOS
  237.  
  238. #ifndef PAGER
  239. #define PAGER "more"
  240. #endif
  241. #ifndef PAGER_ARGS
  242. #define PAGER_ARGS NULL
  243. #endif
  244.  
  245.       int diff_stdin;
  246.       int pager_stdin;
  247.  
  248.       fclose (outfile);
  249.  
  250.       diff_stdin = dup (0);
  251.       pager_stdin = open (_pipe_file (), O_RDONLY|O_TEXT);
  252.       dup2 (pager_stdin, 0);
  253.  
  254.       spawnlp (P_WAIT, PAGER, PAGER, PAGER_ARGS, NULL);
  255.  
  256.       dup2 (diff_stdin, 0);
  257.       close (diff_stdin);
  258.       close (pager_stdin);
  259.  
  260.       printf ("hit any key to continue...");
  261.       while (!kbhit ())
  262.     ;
  263.  
  264.       printf ("\n");
  265.  
  266. #else /* not MSDOS */
  267.  
  268.       fclose (outfile);
  269.       wait (0);
  270.  
  271. #endif /* not MSDOS */
  272.     }
  273. }
  274.  
  275. /* Compare two lines (typically one from each input file)
  276.    according to the command line options.
  277.    Each line is described by a `struct line_def'.
  278.    Return 1 if the lines differ, like `bcmp'.  */
  279.  
  280. int
  281. line_cmp (s1, s2)
  282.      struct line_def *s1, *s2;
  283. {
  284.   register char _huge *t1, _huge *t2;
  285.   register char end_char = line_end_char;
  286. #ifdef MSDOS
  287.   char savechar;
  288. #else /* not MSDOS */
  289.   int savechar;
  290. #endif /* not MSDOS */
  291.  
  292.   /* Check first for exact identity.
  293.      If that is true, return 0 immediately.
  294.      This detects the common case of exact identity
  295.      faster than complete comparison would.  */
  296.  
  297.   t1 = s1->text;
  298.   t2 = s2->text;
  299.  
  300.   /* Alter the character following line 2 so it doesn't
  301.      match that following line 1.
  302.      (We used to alter the character after line 1,
  303.      but that caused trouble if line 2 directly follows line 1.)  */
  304.   savechar = s2->text[s2->length];
  305.   s2->text[s2->length] = s1->text[s1->length] + 1;
  306.  
  307.   /* Now find the first mismatch; this won't go past the
  308.      character we just changed.  */
  309.   while (*t1++ == *t2++);
  310.  
  311.   /* Undo the alteration.  */
  312.   s2->text[s2->length] = savechar;
  313.  
  314.   /* If the comparison stopped at the alteration,
  315.      the two lines are identical.  */
  316.   if (t2 == s2->text + s2->length + 1)
  317.     return 0;
  318.  
  319.   /* Not exactly identical, but perhaps they match anyway
  320.      when case or whitespace is ignored.  */
  321.  
  322.   if (ignore_case_flag || ignore_space_change_flag || ignore_all_space_flag)
  323.     {
  324.       t1 = s1->text;
  325.       t2 = s2->text;
  326.  
  327.       while (1)
  328.     {
  329.       register char c1 = *t1++;
  330.       register char c2 = *t2++;
  331.  
  332.       /* Ignore horizontal whitespace if -b or -w is specified.  */
  333.  
  334.       if (ignore_all_space_flag)
  335.         {
  336.           /* For -w, just skip past any spaces or tabs.  */
  337.           while (c1 == ' ' || c1 == '\t') c1 = *t1++;
  338.           while (c2 == ' ' || c2 == '\t') c2 = *t2++;
  339.         }
  340.       else if (ignore_space_change_flag)
  341.         {
  342.           /* For -b, advance past any sequence of whitespace in line 1
  343.          and consider it just one Space, or nothing at all
  344.          if it is at the end of the line.  */
  345.           if (c1 == ' ' || c1 == '\t')
  346.         {
  347.           while (1)
  348.             {
  349.               c1 = *t1++;
  350.               if (c1 == end_char)
  351.             break;
  352.               if (c1 != ' ' && c1 != '\t')
  353.             {
  354.               --t1;
  355.               c1 = ' ';
  356.               break;
  357.             }
  358.             }
  359.         }
  360.  
  361.           /* Likewise for line 2.  */
  362.           if (c2 == ' ' || c2 == '\t')
  363.         {
  364.           while (1)
  365.             {
  366.               c2 = *t2++;
  367.               if (c2 == end_char)
  368.             break;
  369.               if (c2 != ' ' && c2 != '\t')
  370.             {
  371.               --t2;
  372.               c2 = ' ';
  373.               break;
  374.             }
  375.             }
  376.         }
  377.         }
  378.  
  379.       /* Upcase all letters if -i is specified.  */
  380.  
  381.       if (ignore_case_flag)
  382.         {
  383.           if (islower (c1))
  384.         c1 = toupper (c1);
  385.           if (islower (c2))
  386.         c2 = toupper (c2);
  387.         }
  388.  
  389.       if (c1 != c2)
  390.         break;
  391.       if (c1 == end_char)
  392.         return 0;
  393.     }
  394.     }
  395.  
  396.   return (1);
  397. }
  398.  
  399. /* Find the consecutive changes at the start of the script START.
  400.    Return the last link before the first gap.  */
  401.  
  402. struct change *
  403. find_change (start)
  404.      struct change *start;
  405. {
  406.   return start;
  407. }
  408.  
  409. struct change *
  410. find_reverse_change (start)
  411.      struct change *start;
  412. {
  413.   return start;
  414. }
  415.  
  416. /* Divide SCRIPT into pieces by calling HUNKFUN and
  417.    print each piece with PRINTFUN.
  418.    Both functions take one arg, an edit script.
  419.  
  420.    HUNKFUN is called with the tail of the script
  421.    and returns the last link that belongs together with the start
  422.    of the tail.
  423.  
  424.    PRINTFUN takes a subscript which belongs together (with a null
  425.    link at the end) and prints it.  */
  426.  
  427. #ifdef MSDOS
  428. void
  429. print_script (    struct change *script,
  430.         struct change * (*hunkfun) (struct change *),
  431.         void (*printfun) (struct change *) )
  432. #else
  433. void
  434. print_script (script, hunkfun, printfun)
  435.      struct change *script;
  436.      struct change * (*hunkfun) ();
  437.      void (*printfun) ();
  438. #endif /* MSDOS */
  439. {
  440.   struct change *next = script;
  441.  
  442.   while (next)
  443.     {
  444.       struct change *this, *end;
  445.  
  446.       /* Find a set of changes that belong together.  */
  447.       this = next;
  448.       end = (*hunkfun) (next);
  449.  
  450.       /* Disconnect them from the rest of the changes,
  451.      making them a hunk, and remember the rest for next iteration.  */
  452.       next = end->link;
  453.       end->link = NULL;
  454. #ifdef DEBUG
  455.       debug_script (this);
  456. #endif
  457.  
  458.       /* Print this hunk.  */
  459.       (*printfun) (this);
  460.  
  461.       /* Reconnect the script so it will all be freed properly.  */
  462.       end->link = next;
  463.     }
  464. }
  465.  
  466. /* Print the text of a single line LINE,
  467.    flagging it with the characters in LINE_FLAG (which say whether
  468.    the line is inserted, deleted, changed, etc.).  */
  469.  
  470. void
  471. print_1_line (line_flag, line)
  472.      char *line_flag;
  473.      struct line_def *line;
  474. {
  475.   int length = line->length; /* must be nonzero */
  476.   const char *text = line->text; /* Help the compiler.  */
  477.   FILE *out = outfile; /* Help the compiler some more.  */
  478.  
  479.   /* If -T was specified, use a Tab between the line-flag and the text.
  480.      Otherwise use a Space (as Unix diff does).
  481.      Print neither space nor tab if line-flags are empty.  */
  482.  
  483.   if (line_flag != NULL && line_flag[0] != 0)
  484.     fprintf (out, tab_align_flag ? "%s\t" : "%s ", line_flag);
  485.  
  486.   /* Now output the contents of the line.
  487.      If -t was specified, expand tabs to spaces.
  488.      Otherwise output verbatim.  */
  489.  
  490.   if (tab_expand_flag)
  491.     {
  492.       register int column = 0;
  493.       register int i;
  494.       for (i = 0; i < line->length; i++)
  495.     {
  496.       register char c = line->text[i];
  497.       switch (c)
  498.         {
  499.         case '\t':
  500.           column++;
  501.           while (column & 7)
  502.         {
  503.           putc (' ', out);
  504.           column++;
  505.         }
  506.           c = ' ';
  507.           break;
  508.         case '\b':
  509.           column--;
  510.           break;
  511.         default:
  512.           column++;
  513.           break;
  514.         }
  515.       putc (c, out);
  516.     }
  517.     }
  518.   else
  519.     fwrite (text, sizeof (char), length, out);
  520.   if ((line_flag == NULL || line_flag[0] != 0) && text[length - 1] != '\n'
  521.       && line_end_char == '\n')
  522.     fprintf (out, "\n\\ No newline at end of file\n");
  523. }
  524.  
  525. change_letter (inserts, deletes)
  526.      int inserts, deletes;
  527. {
  528.   if (!inserts)
  529.     return 'd';
  530.   else if (!deletes)
  531.     return 'a';
  532.   else
  533.     return 'c';
  534. }
  535.  
  536. /* Translate an internal line number (an index into diff's table of lines)
  537.    into an actual line number in the input file.
  538.    The internal line number is LNUM.  FILE points to the data on the file.
  539.  
  540.    Internal line numbers count from 0 within the current chunk.
  541.    Actual line numbers count from 1 within the entire file;
  542.    in addition, they include lines ignored for comparison purposes.
  543.  
  544.    The `ltran' feature is no longer in use.  */
  545.  
  546. int
  547. translate_line_number (file, lnum)
  548.      struct file_data *file;
  549.      int lnum;
  550. {
  551.   return lnum + 1;
  552. }
  553.  
  554. void
  555. translate_range (file, a, b, aptr, bptr)
  556.      struct file_data *file;
  557.      int a, b;
  558.      int *aptr, *bptr;
  559. {
  560.   *aptr = translate_line_number (file, a - 1) + 1;
  561.   *bptr = translate_line_number (file, b + 1) - 1;
  562. }
  563.  
  564. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  565.    If the two numbers are identical, print just one number.
  566.  
  567.    Args A and B are internal line numbers.
  568.    We print the translated (real) line numbers.  */
  569.  
  570. void
  571. print_number_range (sepchar, file, a, b)
  572.      char sepchar;
  573.      struct file_data *file;
  574.      int a, b;
  575. {
  576.   int trans_a, trans_b;
  577.   translate_range (file, a, b, &trans_a, &trans_b);
  578.  
  579.   /* Note: we can have B < A in the case of a range of no lines.
  580.      In this case, we should print the line number before the range,
  581.      which is B.  */
  582.   if (trans_b > trans_a)
  583.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  584.   else
  585.     fprintf (outfile, "%d", trans_b);
  586. }
  587.  
  588. /* Look at a hunk of edit script and report the range of lines in each file
  589.    that it applies to.  HUNK is the start of the hunk, which is a chain
  590.    of `struct change'.  The first and last line numbers of file 0 are stored in
  591.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1. 
  592.    Note that these are internal line numbers that count from 0.
  593.  
  594.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  595.  
  596.    Also set *DELETES nonzero if any lines of file 0 are deleted
  597.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  598.    If only ignorable lines are inserted or deleted, both are
  599.    set to 0.  */
  600.  
  601. void
  602. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  603.      struct change *hunk;
  604.      int *first0, *last0, *first1, *last1;
  605.      int *deletes, *inserts;
  606. {
  607.   int f0, l0, f1, l1, show_from, show_to;
  608.   int i;
  609.   int nontrivial = !(ignore_blank_lines_flag || ignore_regexp);
  610.   struct change *next;
  611.  
  612.   show_from = show_to = 0;
  613.  
  614.   f0 = hunk->line0;
  615.   f1 = hunk->line1;
  616.  
  617.   for (next = hunk; next; next = next->link)
  618.     {
  619.       l0 = next->line0 + next->deleted - 1;
  620.       l1 = next->line1 + next->inserted - 1;
  621.       show_from += next->deleted;
  622.       show_to += next->inserted;
  623.  
  624.       for (i = next->line0; i <= l0 && ! nontrivial; i++)
  625.     if ((!ignore_blank_lines_flag || files[0].linbuf[i].length > 1)
  626.         && (!ignore_regexp
  627.         || 0 > re_search (&ignore_regexp_compiled,
  628.                   files[0].linbuf[i].text,
  629.                   files[0].linbuf[i].length, 0,
  630.                   files[0].linbuf[i].length, 0)))
  631.       nontrivial = 1;
  632.  
  633.       for (i = next->line1; i <= l1 && ! nontrivial; i++)
  634.     if ((!ignore_blank_lines_flag || files[1].linbuf[i].length > 1)
  635.         && (!ignore_regexp
  636.         || 0 > re_search (&ignore_regexp_compiled,
  637.                   files[1].linbuf[i].text,
  638.                   files[1].linbuf[i].length, 0,
  639.                   files[1].linbuf[i].length, 0)))
  640.       nontrivial = 1;
  641.     }
  642.  
  643.   *first0 = f0;
  644.   *last0 = l0;
  645.   *first1 = f1;
  646.   *last1 = l1;
  647.  
  648.   /* If all inserted or deleted lines are ignorable,
  649.      tell the caller to ignore this hunk.  */
  650.  
  651.   if (!nontrivial)
  652.     show_from = show_to = 0;
  653.  
  654.   *deletes = show_from;
  655.   *inserts = show_to;
  656. }
  657.  
  658. /* malloc a block of memory, with fatal error message if we can't do it. */
  659.  
  660. #ifdef MSDOS
  661. #include <malloc.h>
  662. #include <dos.h>
  663.  
  664. void _huge *
  665. xhalloc (long size)
  666. {
  667.   void _huge *value = (void _huge *) halloc (size, (size_t)1 );
  668.  
  669.   if (!value)
  670.     fatal ("virtual memory exhausted");
  671.   return value;
  672. }
  673.  
  674. /* Here we do a huge "realloc" by allocating a new block and
  675.    moving the old block afterwards.  This is *slow*, but should
  676.    be reliable.  */
  677.  
  678. void _huge *
  679. xhrealloc (void _huge *ptr, long new_size, long old_size)
  680. {
  681.   void _huge *value = (void _huge *) halloc (new_size, (size_t)1 );
  682.  
  683.   if (!value)
  684.     fatal ("virtual memory exhausted");
  685.   else
  686.     {
  687.       char _huge *dest = value;
  688.       char _huge *src = ptr;
  689.  
  690.       while (old_size > 0L)
  691.     {
  692.       unsigned int bytes = (unsigned int) min (0x8000L, old_size);
  693.       memcpy (dest, src, bytes);
  694.       old_size -= (long) bytes;
  695.       dest += (long) bytes;
  696.       src += (long) bytes;
  697.     }
  698.     }
  699.   hfree (ptr);
  700.   return value;
  701. }
  702.  
  703. long        /* doesn't belong here, but is also a 'huge' pointer kludge */
  704. hread (int fd, void _huge *buffer, long bytes)
  705. {
  706.   long bytes_read = 0L;
  707.  
  708.   while (1)
  709.     {
  710.       int n = read (fd, buffer, (unsigned int) min (0x4000L, bytes));
  711.  
  712.       if (n > 0)
  713.     {
  714.       bytes_read += (long) n;
  715.       bytes -= (long) n;
  716.       /* we can't say buffer += n here, because we have to make
  717.          sure that the offset of BUFFER doesn't overflow during
  718.          the read() system call.  Therefore we add what we read
  719.          to the segment of BUFFER.  */
  720.       FP_SEG(buffer) += (n >> 4);
  721.       FP_OFF(buffer) += (n & 0x0F);
  722.     }
  723.       else if (n == 0)
  724.     return bytes_read;    /* done */
  725.       else
  726.     pfatal_with_name ("error while reading input");
  727.     }
  728. }
  729.  
  730. #endif /* MSDOS */
  731.  
  732. VOID *
  733. xmalloc (size)
  734.      unsigned size;
  735. {
  736.   register VOID *value;
  737.  
  738.   if (size == 0)
  739.     size = 1;
  740.  
  741.   value = (VOID *) malloc (size);
  742.  
  743.   if (!value)
  744.     fatal ("virtual memory exhausted");
  745.   return value;
  746. }
  747.  
  748. /* realloc a block of memory, with fatal error message if we can't do it. */
  749.  
  750. VOID *
  751. xrealloc (old, size)
  752.      VOID *old;
  753.      unsigned int size;
  754. {
  755.   register VOID *value;
  756.  
  757.   if (size == 0)
  758.     size = 1;
  759.  
  760.   value = (VOID *) realloc (old, size);
  761.  
  762.   if (!value)
  763.     fatal ("virtual memory exhausted");
  764.   return value;
  765. }
  766.  
  767. /* Concatenate three strings, returning a newly malloc'd string.  */
  768.  
  769. char *
  770. concat (s1, s2, s3)
  771.      char *s1, *s2, *s3;
  772. {
  773.   int len = strlen (s1) + strlen (s2) + strlen (s3);
  774.   char *new = (char *) xmalloc (len + 1);
  775.   strcpy (new, s1);
  776.   strcat (new, s2);
  777.   strcat (new, s3);
  778.   return new;
  779. }
  780.  
  781. #ifdef MSDOS
  782. void
  783. #endif /* MSDOS */
  784. debug_script (sp)
  785.      struct change *sp;
  786. {
  787.   fflush (stdout);
  788.   for (; sp; sp = sp->link)
  789.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  790.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  791.   fflush (stderr);
  792. }
  793.  
  794. #ifdef MSDOS
  795.  
  796. static char *pipe_file = NULL;
  797. static char *tmpdir = NULL;
  798. static int tmpdirlen;
  799.  
  800. /* Return the name of a temporary file.  */
  801. char *
  802. _pipe_file (void)
  803. {
  804.   if (!pipe_file)
  805.     {
  806.       if (!tmpdir)
  807.     {
  808.       /* Initialize.  */
  809.  
  810.       atexit (cleanup_pipe);
  811.  
  812.       tmpdir = getenv ("TMP");
  813.  
  814.       if (tmpdir)
  815.         {
  816.           tmpdirlen = strlen (tmpdir);
  817.           if (tmpdir[tmpdirlen - 1] == '/'
  818.           || tmpdir[tmpdirlen - 1] == '\\')
  819.         tmpdir[tmpdirlen - 1] = '\0';
  820.         }
  821.       else
  822.         {
  823.           tmpdir = ".";
  824.           tmpdirlen = 1;
  825.         }
  826.     }
  827.  
  828.       pipe_file = (char *) xmalloc (tmpdirlen + 14);
  829.       sprintf (pipe_file, "%s/diff%04x", tmpdir, getpid ());
  830.     }
  831.  
  832.   return pipe_file;
  833. }
  834.  
  835.  
  836. /* Clean up after we are done. */
  837.  
  838. void
  839. cleanup_pipe (void)
  840. {
  841.   unlink (pipe_file);
  842. }
  843.  
  844. #endif /* MSDOS */
  845.  
  846.